//
//  JingxuanScrollView.m
//  JingXuanTableView
//
//  Created by TianZhen on 16/4/6.
//  Copyright © 2016年 TianZhen. All rights reserved.
//
#import "UIImageView+WebCache.h"
#import "JingxuanScrollView.h"

@interface JingxuanScrollView ()
@property (nonatomic,strong) NSMutableArray *imageViews;
@end

@implementation JingxuanScrollView

#pragma mark - ======== 缓存 ========
- (NSMutableArray *)imageViews
{
    if (!_imageViews) {
        _imageViews = [NSMutableArray array];
    }
    return _imageViews;
}

- (void)setImages:(NSArray *)images
{
    _images = images;
    
    [self setup];
}

- (void)setup
{
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    // 如果有ImageView
    if (self.imageViews.count != 0) {
        
        // 清空图片
        [self.imageViews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger index, BOOL * _Nonnull stop) {
            imageView.image = nil;
        }];
        
        // 索引
        NSInteger index = 0;
        // 重新赋值图片
        // 如果是字符串
        if ([self.images.firstObject isKindOfClass:[NSString class]]) {
            for (NSString *imageURLString in self.images) {
                
                NSURL *imageURL = [NSURL URLWithString:imageURLString];
                // 拿到图片view
                UIImageView *imageView = self.imageViews[index];
                // 赋值
                [imageView sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:@"私人订制1"]];
                index++;
                if (index > self.imageViews.count-1) {
                    break;
                }
            }
        }else
        {// 如果是图片
            for (UIImage *image in self.images) {
                // 拿到图片view
                UIImageView *imageView = self.imageViews[index];
                // 赋值
                imageView.image = image;
                index ++;
            }
        }

        
    }else
    {
        // 遍历图片数组
        [self.images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger index, BOOL * _Nonnull stop) {

            // 创建图片view
            UIImageView *imageView = [[UIImageView alloc] init];
            
            // 如果是图片
            if ([image isKindOfClass:[UIImage class]]) {
                imageView.image = image;
            }else
            {
                NSURL *URL = [NSURL URLWithString:(NSString *)image];
                [imageView sd_setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"私人订制1"]];
            }

            // 添加
            [self addSubview:imageView];
            
            // 添加到缓存
            [self.imageViews addObject:imageView];
            
            // 给imageView添加手势
        }];

    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat margin = 2;
    CGFloat weight = self.bounds.size.height - margin;
    CGFloat height = weight;
    
    CGFloat y = (self.bounds.size.height - height) *0.5;
    NSInteger i = 0;
    
    CGFloat maxX = 0;
    for (UIImageView *imageView in self.subviews) {
        
        CGFloat x = margin + i * (weight + margin);
        imageView.frame = CGRectMake(x, y, weight, height);
        
        i ++;
        
        maxX = CGRectGetMaxX(imageView.frame);
    }
    
    self.contentSize = CGSizeMake(maxX, 0);
}
@end
